package com.shanghaionstar.user;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.more.entity.ContentHeaderType;
import com.onstar.cn.more.response.GetHelpListResponseType;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.user.adapter.CommonAdapter;
import com.shanghaionstar.user.adapter.ViewHolder;
import com.shanghaionstar.user.iview.IHelpListView;
import com.shanghaionstar.user.presenter.HelpListPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by 7828wc on 16/7/1.
 */
public class HelpListFragment extends BaseFragment implements IHelpListView {
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private ListView help_list;

    @FindViewById
    private ProgressLayout progress_layout;

    private HelpListPresenter presenter;


    private MoreActivity parentActivity;

    private BaseActivity activity;


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public int getLayoutId() {
        return R.layout.more_help_list;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        if (isFirstLoad()) {
            super.onActivityCreated(savedInstanceState);
            parentActivity = (MoreActivity) getActivity();
            presenter = new HelpListPresenter(parentActivity.getScheduler());
            presenter.setView(this);
            presenter.start();
        }

    }

    @Override
    public void setLoading(boolean isLoading) {
        if (isLoading) progress_layout.setVisibility(View.VISIBLE);
        else progress_layout.setVisibility(View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(onActivity(), e);
    }

    @Override
    public void onReponseSuccess(final GetHelpListResponseType object) {
        try {
            help_list.setAdapter(new CommonAdapter<ContentHeaderType>(parentActivity, object.getContentHeaderList(), R.layout.onstar_fg_help_item) {
                @Override
                public void convert(ViewHolder holder, ContentHeaderType contentHeaderType) {
                    holder.setText(R.id.item_tv, contentHeaderType.getTitle());
                }
            });

         /*   help_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    presenter.onItemClick(position,object, parentActivity);
                }
            });*/

            help_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    presenter.onItemClick(position, object, parentActivity);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }

}
